import { React, toNode } from 'DoraX';
import { Director, Node, Sprite, Size, App, Vec2, View, tolua, TypeName, Camera2D, Label, TextAlign, Color, sleep, Path, Content, Routine, KeyName, ButtonName, wait, threadLoop } from 'Dora';
import { Button } from 'Script/UI/Button';
import { DB, thread, SQL, Audio } from "Dora";
import { SaveManager, actName } from "Script/SaveManager";
import { playAnimation, NodeMove } from "Script/Animation/Animation";
import { designSize, winsize, fontName, appState, Illustration, Savefile, defaultnpcprops } from "Script/config";
import { CreateManager, GamePad, Trigger } from 'InputManager';
import { Transition } from 'Script/Animation/Transition'
import { Dialog } from 'Script/Dialog'

let nowslot = 0;
let nowpage = 0;
let gamestarttime = 0;
let canskip = false;
let skip = false;
let player_state = {
	A: 0,
	D: 0,
	B: 0,
	TAB: 0,
};
let laststate = {
	A: 0,
	D: 0,
	B: 0,
	TAB: 0,
}

laststate = { ...player_state };
let canmove = false;
const walkspeed = 8;
// const walkspeed = 5;

const Pressed = (keyName: KeyName, buttonName: ButtonName) => {
	return Trigger.Selector([
		Trigger.KeyPressed(keyName),
		Trigger.ButtonPressed(buttonName)
	]);
};

const inputManager = CreateManager({
	Game: {
		Up: Trigger.Selector([Pressed(KeyName.W, ButtonName.Up), Trigger.KeyPressed(KeyName.Up)]),
		Down: Trigger.Selector([Pressed(KeyName.S, ButtonName.Down), Trigger.KeyPressed(KeyName.Down)]),
		LeftDown: Trigger.Selector([Trigger.KeyDown(KeyName.Left), Trigger.KeyDown(KeyName.A)]),
		RightDown: Trigger.Selector([Trigger.KeyDown(KeyName.Right), Trigger.KeyDown(KeyName.D)]),
		LeftUp: Trigger.Selector([Trigger.KeyUp(KeyName.Left), Trigger.KeyUp(KeyName.A)]),
		RightUp: Trigger.Selector([Trigger.KeyUp(KeyName.Right), Trigger.KeyUp(KeyName.D)]),
		EDown: Trigger.Selector([Trigger.KeyDown(KeyName.E)]),
		EUp: Trigger.Selector([Trigger.KeyUp(KeyName.E)]),
		Space: Trigger.Selector([Trigger.KeyDown(KeyName.Space)]),
		Esc: Trigger.Selector([Trigger.KeyDown(KeyName.Escape)]),
	},
	UI: {
		Start: Trigger.Selector([
			Trigger.KeyDown(KeyName.Return),
			Trigger.ButtonDown(ButtonName.Start)
		]),
	}
});

inputManager.getNode().addTo(Director.ui);

toNode(
	<GamePad inputManager={inputManager} noLeftStick noRightStick noButtonPad noTriggerPad noControlPad noDPad />
)?.addTo(Director.ui);

// 更新视图大小
const updateViewSize = () => {
	const camera = tolua.cast(Director.currentCamera, TypeName.Camera2D);
	if (camera) {
		camera.zoom = Math.min(View.size.height / designSize.height, View.size.width / designSize.width);
	}
};

// 自动调整窗口
const adjustWinSize = () => {
	App.winSize = winsize;
	// print(`Visual size: ${App.visualSize}`);
};
//存档操作
const file_num = 4;
const saveManager = new SaveManager();
let savefiles = saveManager.getAllSaves(file_num);
let savefile = savefiles[0];
let npcprops = defaultnpcprops;
const initnpcvisible = () => {
	npcprops = defaultnpcprops;
}
initnpcvisible();

//音频
const resources = {
	textures: [],
	models: [],
	sounds: {
		end: "Audio/twilight.mp3",
		back: "Audio/loop.mp3",
	}
};

Audio.playStream(resources.sounds.back, true);

// 启动场景
const StartUp = () => {
	nowpage = 0;
	inputManager.popContext();
	inputManager.pushContext('UI');
	const buttonWidth = 312 - 20;
	const buttonHeight = 124 - 20;
	const buttonx = 0;
	let temp = 7
	const buttony: number[] = [
		designSize.height / temp * 3 - designSize.height / 2 - 20,
		designSize.height / temp * 1.5 - designSize.height / 2 - 20
	];
	savefiles = saveManager.getAllSaves(file_num);
	savefile = savefiles[0];

	const newGameClick = (switched?: boolean, tag?: number) => {
		if (appState.isCGing) { return }
		savefile = saveManager.createDefaultSave();
		initnpcvisible();
		appState.isCGing = true;
		Transition({
			root: Director.entry,
			time: 1.5,
			type: 1,
			waittime: 0.7,
		})
		appState.isCGing = false;
		root.cleanup();
		root.removeAllChildren();
		root.removeFromParent();
		Game();
	};

	const exitClick = (switched?: boolean, tag?: number) => {
		if (appState.isCGing) { return }
		appState.isCGing = true;
		Transition({
			root: Director.entry,
			time: 1,
			type: -1,
			waittime: 1,
		})
		App.shutdown();

	};

	const root = Node();
	const background = Sprite("Image/部件/beijing.png") ?? Sprite();
	background.position = Vec2(0, 0)
	root.addChild(background);
	const title = Sprite("Image/部件/biaoti.png") ?? Sprite();
	title.position = Vec2(0, 180);
	root.addChild(title);

	//按钮1
	const bt1 = Button({
		x: buttonx,
		y: buttony[0],
		width: buttonWidth,
		height: buttonHeight,
		onClick: newGameClick,
		normalImage: "Image/部件/kaishi.png",
		pressImage: "Image/部件/kaishi.png",
		// text: "新游戏",
	});

	// 按钮4
	const bt2 = Button({
		x: buttonx,
		y: buttony[1],
		width: buttonWidth,
		height: buttonHeight,
		onClick: exitClick,
		normalImage: "Image/部件/jieshu.png",
		pressImage: "Image/部件/jieshu.png",
		// text: "退出游戏",
	});

	root.addChild(bt1.node);
	root.addChild(bt2.node);

	return root;
};

const Game = () => {

	gamestarttime = App.totalTime;
	inputManager.popContext();
	inputManager.pushContext('Game');

	const root = Node();

	act_zero();
	return root;
}

const act_zero = () => {

	nowpage = 0;
	savefile.act = 0;
	saveManager.saveGame(nowslot, savefile);
	const root = Node();
	appState.isCGing = true;
	Transition({
		root: Director.entry,
		time: 1,
		type: -1,
		waittime: 0,
	})
	const finish = (time: number) => {
		savefile.playTime = savefile.playTime + App.totalTime - gamestarttime;
		saveManager.saveGame(nowslot, savefile);

		thread(() => {
			appState.isCGing = true;
			Transition({
				root: Director.entry,
				time: time,
				type: -1,
				waittime: 0,
			})
			appState.isCGing = false;
			root.cleanup();
			root.removeAllChildren();
			root.removeFromParent();
		})
	}

	//剧情
	thread(() => {
		const eButton = Button({
			x: 320,
			y: 300,
			width: 80,
			height: 80,
			onClick: () => {
				skip = true;
			},
			normalImage: "Image/部件/E.png",
			pressImage: "Image/部件/E.png",
		});
		eButton.node.visible=false;

		//展开信纸
		const xinzhi = Sprite("Image/第一幕/zhi.png");
		if (xinzhi) {
			xinzhi.size = Size(600, 660);
			xinzhi.onTapped(() => {
				skip = true;
			})
		}
		const xincontent = Label(fontName, 30);
		if (xincontent) {
			xincontent.text = "    我是云忆，精神研究所所长。\n    我记录下这一切，以防止人们再一次抛弃“桥”。\n    记忆断桥症，是一种会让人遗忘与“桥”相关的一切记忆的病症。\n    它让人类失去了对记忆的掌控。\n    记忆，不再是奔腾的河流，而是断层的云。\n    而我，是那个试图在云中修桥的人。\n    故事发生在一年前。\n    这一切因我而起，也同样，因我而终。";
			xincontent.textWidth = 500;
			xincontent.color = Color(0xff000000)
			xincontent.alignment = TextAlign.Left;
		}
		eButton.node.x=320;
		eButton.node.y=300;
		eButton.node.visible=true;

		sleep(4);

		eButton.node.gslot("Input.EDown", () => {
			skip = true;
		});
		
		canskip = true;
		wait(() => { sleep(0.1); return skip; });
		skip = false; canskip = false;
		eButton.node.removeFromParent();
		if (xinzhi)
			xinzhi.removeFromParent();
		if (xincontent)
			xincontent.removeFromParent();
		
		const dialognode = Dialog({
			name: "",
			illustration: Illustration.wu,
		})
		root.addChild(dialognode.node);

		dialognode.setname("？？？");
		dialognode.setcontent("所长，您醒了吗？");
		
		sleep(1);

		canskip = true;
		wait(() => {
			return skip;
		});
		skip = false; canskip = false;
		
		dialognode.setname("");
		dialognode.setcontent("————一道声音穿透意识的迷雾抵达耳畔，似乎是个年轻的女声。");
		
		sleep(1);

		canskip = true;
		wait(() => {
			return skip;
		});
		skip = false; canskip = false;
		
		dialognode.setname("云忆");
		dialognode.setcontent("头好晕……果然还是不该勉强自己吗……");
		
		sleep(1);

		canskip = true;
		wait(() => {
			return skip;
		});
		skip = false; canskip = false;
		
		dialognode.setname("");
		dialognode.setcontent("视野逐渐清晰。红发研究员坐在一尘不染的实验桌前，唇角带着惯常的笑。");
		
		sleep(1);

		canskip = true;
		wait(() => {
			return skip;
		});
		skip = false; canskip = false;

		finish(1);
		act_one();
	});
	root.gslot("Input.Space", () => {
		skip = canskip;
	});
	root.onTapped(() => {
		skip = canskip;
	})
	return root;
}

const act_one = () => {

	nowpage = 1;
	savefile.act = 1;
	saveManager.saveGame(nowslot, savefile);
	const root = Node();
	appState.isCGing = true;
	Transition({
		root: Director.entry,
		time: 1,
		type: -1,
		waittime: 0,
	})
	const finish = (time: number) => {
		savefile.playTime = savefile.playTime + App.totalTime - gamestarttime;
		saveManager.saveGame(nowslot, savefile);

		thread(() => {
			appState.isCGing = true;
			Transition({
				root: Director.entry,
				time: time,
				type: -1,
				waittime: 0,
			})
			appState.isCGing = false;
			root.cleanup();
			root.removeAllChildren();
			root.removeFromParent();
		})
	}

	appState.isCGing = false;
	const background = Sprite("Image/第一幕/official/home_back.png") ?? Sprite();
	root.addChild(background);
	const zhujue = Sprite("Image/role/主角.png") ?? Sprite();
	zhujue.position = Vec2(100, -75);
	root.addChild(zhujue);
	const peijue = Sprite("Image/role/配角.png") ?? Sprite();
	peijue.position = Vec2(400, -155);
	root.addChild(peijue);
	const zhuozi = Sprite("Image/第一幕/official/home_desk.png") ?? Sprite();
	zhuozi.position = Vec2(-50, -10);
	root.addChild(zhuozi);

	//剧情
	thread(() => {
		const dialognode = Dialog({
			name: "",
			illustration: Illustration.wu,
		})
		root.addChild(dialognode.node);

		dialognode.setname("？？？");
		dialognode.setcontent("所长，实验感觉如何？");
		
		sleep(1);

		canskip = true;
		wait(() => {
			return skip;
		});
		skip = false; canskip = false;

		dialognode.setname("");
		dialognode.setcontent("是墨染，精神连接研究所最年轻的职员，我最得力的助手，她那一头火红的长发比她的名字更令人印象深刻。");
		
		sleep(1.5);

		canskip = true;
		wait(() => {
			return skip;
		});
		skip = false;
		canskip = false;
		
		dialognode.setname("云忆");
		dialognode.setcontent("很……微妙……算是成功了一半吧，但是……");
		
		sleep(1);

		canskip = true;
		wait(() => {
			return skip;
		});
		skip = false;
		canskip = false;
		
		dialognode.setname("");
		dialognode.setcontent("奇特的感受。头隐隐作痛，喉咙发紧，像刚从宿醉中醒来一样，似乎有什么很重要的事在不受控制地流逝……");
		
		sleep(1.5);

		canskip = true;
		wait(() => {
			return skip;
		});
		skip = false;
		canskip = false;
		
		dialognode.setname("云忆");
		dialognode.setcontent("我们的实验目标，是...什么...");
		
		sleep(1);

		canskip = true;
		wait(() => {
			return skip;
		});
		skip = false;
		
		dialognode.setname("墨染");
		dialognode.setcontent("——嗯？您……忘记了？");
		
		sleep(0.6);

		canskip = true;
		wait(() => {
			return skip;
		});
		skip = false;
		canskip = false;

		dialognode.setname("");
		dialognode.setcontent("即便是一向游刃有余、处变不惊的墨染听到也愣住了，一成不变的笑容出现了一丝裂痕，眼神里是前所未有的震惊。");
		
		sleep(1.5);

		canskip = true;
		wait(() => {
			return skip;
		});
		skip = false;
		canskip = false;

		dialognode.setcontent("毕竟，自己的所长在实验中竟然忘记了实验目标，这放在任何实验中应该都是骇人听闻的———");
		
		sleep(1.5);

		canskip = true;
		wait(() => {
			return skip;
		});
		skip = false;
		canskip = false;

		dialognode.setname("云忆");
		dialognode.setcontent("看来，这次实验的后果，比我预想的还要严重……");
		
		sleep(1);

		canskip = true;
		wait(() => {
			return skip;
		});
		skip = false;
		canskip = false;

		dialognode.setname("");
		dialognode.setcontent("———然而我们的实验，也与以往完全不同。");
		
		sleep(1);

		canskip = true;
		wait(() => {
			return skip;
		});
		skip = false;
		canskip = false;

		dialognode.setcontent("精神连接实验——用实验者的精神去同步、探测物体，在这个崭新的领域中，什么都有可能发生——");
		
		sleep(1.5);

		canskip = true;
		wait(() => {
			return skip;
		});
		skip = false;
		canskip = false;

		dialognode.setname("墨染");
		dialognode.setcontent("这是我记录的实验报告，恐怕所长您不仅仅是失去记忆那么简单……");
		
		sleep(1);

		canskip = true;
		wait(() => {
			return skip;
		});
		skip = false;
		canskip = false;

		dialognode.node.visible = false;
		const eButton = Button({
			x: -130,
			y: -100,
			width: 80,
			height: 80,
			onClick: () => {
				skip = true;
			},
			normalImage: "Image/部件/E.png",
			pressImage: "Image/部件/E.png",
		});
		eButton.node.gslot("Input.EDown", () => {
			skip = true;
		});
		wait(() => { sleep(0.1); return skip; });
		skip = false;
		eButton.node.visible=false;

		//展开信纸
		const xinzhi = Sprite("Image/第一幕/zhi.png");
		if (xinzhi) {
			xinzhi.size = Size(600, 660);
			xinzhi.onTapped(() => {
				skip = true;
			})
		}
		const xincontent = Label(fontName, 30);
		if (xincontent) {
			xincontent.text = "                实验日志\n    实验内容：从精神连接层面观测□的存在\n    受试者：云忆（所长）\n    观测者：墨染\n    实验目的：观测□在受试者精神中的形象，并尝试进行接触\n    实验日期：10月27日\n    实验过程：\n    9：00a.m  实验开始，受试者云忆接入精神连接仪。\n    9：20a.m 面部肌肉放松，推测在回忆与□有关的经历\n    9：27a.m 观测到无声唇语\n    9：29a.m 突然呼吸急促，手指痉挛，反复喃喃念着□，遂手动终止连接，实验结束。在仪器停止后，受试者逐渐恢复正常。";
			xincontent.textWidth = 500;
			xincontent.color = Color(0xff000000)
			xincontent.alignment = TextAlign.Left;
		}
		eButton.node.x=320;
		eButton.node.y=300;
		eButton.node.visible=true;
		
		sleep(5);

		eButton.node.gslot("Input.EDown", () => {
			skip = true;
		});
		
		canskip = true;
		wait(() => { sleep(0.1); return skip; });
		skip = false; canskip = false;
		eButton.node.removeFromParent();
		if (xinzhi)
			xinzhi.removeFromParent();
		if (xincontent)
			xincontent.removeFromParent();

		dialognode.node.visible = true;

		dialognode.setname("");
		dialognode.setcontent("我的目光停在了一个模糊不清的符号上。");
		
		sleep(1);

		canskip = true;
		wait(() => {
			return skip;
		});
		skip = false;
		canskip = false;

		dialognode.setname("云忆");
		dialognode.setcontent("这个图形……是……一个字吗？");
		
		sleep(1);

		canskip = true;
		wait(() => {
			return skip;
		});
		skip = false;
		canskip = false;

		dialognode.setname("墨染");
		dialognode.setcontent("所长，您不认得‘□’字了吗！");
		
		sleep(1);

		canskip = true;
		wait(() => {
			return skip;
		});
		skip = false;
		canskip = false;

		dialognode.setname("");
		dialognode.setcontent("qiao，墨染似乎是这么发音的，但是我却毫无印象。");
		
		sleep(1);

		canskip = true;
		wait(() => {
			return skip;
		});
		skip = false;
		canskip = false;

		dialognode.setcontent("这个字背后的含义、它代表的事物、它的形状……一切与之相关的知识都无迹可寻。有一瞬间，我甚至觉得自己像个文盲。");
		
		sleep(2);

		canskip = true;
		wait(() => {
			return skip;
		});
		skip = false;
		canskip = false;
		
		dialognode.setcontent("真是讽刺，身为所长，却将一个字忘得一干二净。");
		
		sleep(1);

		canskip = true;
		wait(() => {
			return skip;
		});
		skip = false;
		canskip = false;

		dialognode.setname("云忆");
		dialognode.setcontent("看来的确不是简单的失忆，我已经彻底不认识这个字了……");
		
		sleep(1);

		canskip = true;
		wait(() => {
			return skip;
		});
		skip = false;
		canskip = false;

		dialognode.setname("墨染");
		dialognode.setcontent("□是架在空中或水上的通道啊，您的家乡、断□镇，不就是因它而得名的吗？");
		
		sleep(1.5);

		canskip = true;
		wait(() => {
			return skip;
		});
		skip = false;
		canskip = false;

		dialognode.setname("云忆");
		dialognode.setcontent("………………完全没有印象。");
		
		sleep(1);

		canskip = true;
		wait(() => {
			return skip;
		});
		skip = false;
		canskip = false;

		dialognode.setname("墨染");
		dialognode.setcontent("怎么会……不，这不可能！");
		
		sleep(1);

		canskip = true;
		wait(() => {
			return skip;
		});
		skip = false;
		canskip = false;

		dialognode.setname("");
		dialognode.setcontent("我竟然，会对一个字如此束手无策，还是一个与我家乡有关的字……嗯？");
		
		sleep(1.5);

		canskip = true;
		wait(() => {
			return skip;
		});
		skip = false;
		canskip = false;

		dialognode.setname("云忆");
		dialognode.setcontent("既然这个字与我的家乡密不可分——");
		
		sleep(1);

		canskip = true;
		wait(() => {
			return skip;
		});
		skip = false;
		canskip = false;

		dialognode.setname("墨染");
		dialognode.setcontent("不如说您的家乡正是因为□才会存在。");
		
		sleep(1);

		canskip = true;
		wait(() => {
			return skip;
		});
		skip = false;
		canskip = false;
		
		dialognode.setname("");
		dialognode.setcontent("看来也该久违的回趟家了，回到那个因遗忘了这个字而变得陌生的地方。");
		
		sleep(1.5);

		canskip = true;
		wait(() => {
			return skip;
		});
		skip = false;
		canskip = false;

		finish(1);
		act_two();
	});
	root.gslot("Input.Space", () => {
		skip = canskip;
	});
	root.onTapped(() => {
		skip = canskip;
	})
	return root;
}

const act_two = () => {

	nowpage = 2;
	let firstdonghua = false;
	if (savefile.act != 2) {
		savefile.act = 2;
		savefile.player_pos = Vec2(490, 210);
		firstdonghua = true;
	}
	//saveManager.saveGame(nowslot, savefile);

	let guanggaojishu = 0;
	let animatespeed = 1 / walkspeed;
	let etag = -1;

	const background = Sprite("Image/第二幕/official/two_left.png") ?? Sprite();

	const player_size = Size(145, 340);
	let min_player_pos = Vec2(player_size.width / 2, 210);
	let max_player_pos = Vec2(background.width - player_size.width / 2 - 1050, 210);
	//(background.width/2-designSize.width/2,0)-(player_pos.x>=0&&<=background.width-dedesignSizesign.width?player_pos.x:(<0?0:background.width-designSize.width))
	const root = Node();

	const finish = (time: number) => {
		savefile.playTime = savefile.playTime + App.totalTime - gamestarttime;
		thread(() => {
			appState.isCGing = true;
			Transition({
				root: Director.entry,
				time: time,
				type: -1,
				waittime: 0,
			})
			appState.isCGing = false;
			root.cleanup();
			root.removeAllChildren();
			root.removeFromParent();
		})
	}
	
	root.addChild(background);
	let npcs: Node.Type[] = [];
	for (const [key, value] of pairs(npcprops)) {
		const npc = Node();
		npc.tag = value.tag.toString();
		npc.position = value.pos;
		npc.visible = value.visible;
		background.addChild(npc);
		npcs.push(npc);
		playAnimation(npc, value.daiji, 0.4, true, false, Size(175, 350));
	}
	
	const eClick = (switched?: boolean, tag?: number) => {
		if (etag != -1) {
			playAnimation(player_node, "Image/scientist", animatespeed, true, true, player_size);
			player_state.A = 0; player_state.D = 0;
			eButton.node.visible = false;
		}
		if (etag === npcprops.lajitong.tag) {
			if (savefile.progress.indexOf(etag) === -1) {
				playAnimation(player_node, "Image/daiji", 0.4, true, true, player_size);
				thread(() => {
					canmove = false;
					dialognode.setillustration(Illustration.wu);
					dialognode.setname("云忆");
					dialognode.setcontent("唔……河上的究竟是……");
					dialognode.node.visible = true;
					
					sleep(1);

					canskip = true;
					wait(() => {
						return skip;
					});
					skip = false; canskip = false;

					dialognode.setname("");
					dialognode.setcontent("脚下的石板被磨得很光滑，石栏底部生着青苔，拱起的弧度很沉着。");
					
					sleep(1.5);

					canskip = true;
					wait(() => {
						return skip;
					});
					skip = false; canskip = false;

					dialognode.setcontent("视线逐渐上移，弧线在某处毫无征兆地，断了。风拂过缺口，徒留空洞的呜咽。");
					
					sleep(1.5);

					canskip = true;
					wait(() => {
						return skip;
					});
					skip = false; canskip = false;

					dialognode.setcontent("好熟悉的感觉。");
					
					sleep(0.5);

					canskip = true;
					wait(() => {
						return skip;
					});
					skip = false; canskip = false;
					
					dialognode.setcontent("心里像打开了一扇门，但门里一片荒芜。");
					
					sleep(1);

					canskip = true;
					wait(() => {
						return skip;
					});
					skip = false; canskip = false;
					
					dialognode.node.visible = false;
					canmove = true;
					
					max_player_pos = Vec2(background.width - player_size.width / 2 - 550, 210);
				});
			} else {
				thread(() => {
					canmove = false;
					playAnimation(player_node, "Image/daiji", 0.4, true, true, player_size);
					dialognode.setillustration(Illustration.wu);
					dialognode.setname("");
					dialognode.setcontent("往前走吧，看看对面是什么。");

					dialognode.node.visible = true;
					sleep(0.5);

					canskip = true;
					wait(() => {
						return skip;
					});
					skip = false; canskip = false;

					dialognode.node.visible = false;
					canmove = true;
				});

			}
		} else if (etag === npcprops.guanggao1.tag) {
			if (guanggaojishu === 0) {
				thread(() => {
					playAnimation(player_node, "Image/daiji", 0.4, true, true, player_size);
					canmove = false;
					dialognode.setillustration(Illustration.wu);
					dialognode.setname("");
					dialognode.setcontent("——镇子比记忆里荒凉太多太多。");
					dialognode.node.visible = true;
					
					sleep(0.5);
					
					canskip = true;
					wait(() => {
						return skip;
					});
					skip = false; canskip = false;

					dialognode.setcontent("我模糊的记忆分明是这样的：树的碎影悠悠晃着，一声渺远的船号惊飞白鹭，跃动的光点随着一圈圈水波渐渐远去。");
					
					sleep(2);

					canskip = true;
					wait(() => {
						return skip;
					});
					skip = false; canskip = false;

					dialognode.setcontent("偶尔起了阵微风，携着草木的清香直入心脾。岸边人来人往，小贩的吆喝声混杂着笑声，尽显热闹……");
										
					sleep(2);

					canskip = true;
					wait(() => {
						return skip;
					});
					skip = false; canskip = false;

					dialognode.setcontent("还有，河上该有的、被氤氲水汽包裹的……");
										
					sleep(1);

					canskip = true;
					wait(() => {
						return skip;
					});
					skip = false; canskip = false;

					dialognode.node.visible = false;
					canmove = true;
					
					max_player_pos = Vec2(background.width - player_size.width / 2 - 900, 210);
				});
			} 
		} else if (etag === npcprops.dianxiangan.tag) {
			thread(() => {
				canmove = false;
				playAnimation(player_node, "Image/daiji", 0.4, true, true, player_size);
				dialognode.setillustration(Illustration.wu);
				dialognode.setname("");
				dialognode.setcontent("这个啊，最早是木质的。后来洪水冲断过一次，镇子也有了名字，人们聚集起来，修了石质的，人和车都从上面经过。");
				dialognode.node.visible = true;

				sleep(2);

				canskip = true;
				wait(() => {
					return skip;
				});
				skip = false; canskip = false;

				dialognode.setcontent("再后来啊，就塌了，早没人修啦……叫什么来着？唉，算啦算啦……");
													
				sleep(1.5);

				canskip = true;
				wait(() => {
					return skip;
				});
				skip = false; canskip = false;

				dialognode.setname("云忆")
				dialognode.setcontent("我想，我该想办法去对面看看。");
													
				sleep(1);

				canskip = true;
				wait(() => {
					return skip;
				});
				skip = false; canskip = false;

				dialognode.setname("")
				dialognode.setcontent("突然，我感到一阵头晕。");
													
				sleep(0.5);

				canskip = true;
				wait(() => {
					return skip;
				});
				skip = false; canskip = false;

				finish(1);
				act_three();

				savefile.progress.push(npcprops.dianxiangan.tag)
			});
		} 
	}
	const eButton = Button({
		x: 0,
		y: 50,
		width: 128,
		height: 128,
		onClick: eClick,
		normalImage: "Image/部件/E.png",
		pressImage: "Image/部件/E.png",
	});
	eButton.node.gslot("Input.EUp", () => {
		if (eButton.node.visible)
			eClick();
	})
	eButton.node.visible = false;
	eButton.node.addTo(root);

	let setbackgroundposition = () => {
		background.x = background.width / 2 - designSize.width / 2 - ((savefile.player_pos.x - designSize.width / 2) >= 0 && savefile.player_pos.x <= background.width - designSize.width / 2 ? (savefile.player_pos.x - designSize.width / 2) : ((savefile.player_pos.x - designSize.width / 2) < 0 ? 0 : background.width - designSize.width));
	}
	setbackgroundposition();

	const dialognode = Dialog({
		name: "",
		illustration: Illustration.wu,
	})
	root.addChild(dialognode.node);
	dialognode.node.order = 1;

	const player_node = Node();
	player_node.addTo(background);
	player_node.position = Vec2(700, 210);

	dialognode.node.visible = false;
	canmove = true;
	if (firstdonghua) {
		thread(() => {
			canmove = false; dialognode.node.visible = true;
			playAnimation(player_node, "Image/daiji", 0.4, true, true, player_size);

			dialognode.setcontent("（车门关闭的闷响，引擎声渐远）");
												
			sleep(0.5);
			
			canskip = true;
			wait(() => {
				return skip;
			});
			skip = false; canskip = false;

			dialognode.setname("云忆")
			dialognode.setcontent("……断□镇——熟悉又陌生的名字。先随便转转吧。");
															
			sleep(1);

			canskip = true;
			wait(() => {
				return skip;
			});
			skip = false; canskip = false;

			const zhiying = Label(fontName, 30);
			if (zhiying) {
				zhiying.text = "按AD左右移动";
				zhiying.color = Color(0xff000000);
				zhiying.addTo(background);
				zhiying.position = Vec2(savefile.player_pos.x, savefile.player_pos.y + 280);
				thread(() => {
					sleep(2);
					zhiying.removeFromParent();
				})
			}
			dialognode.node.visible = false;
			canmove = true;
		})
	}

	playAnimation(player_node, "Image/scientist", animatespeed, true, true, player_size);
	player_state = {
		A: 0,
		D: 0,
		B: 0,
		TAB: 0,
	};
	laststate = {
		A: 0,
		D: 0,
		B: 0,
		TAB: 0,
	}
	let faceto = true;
	const tl1 = threadLoop(() => {
		if (laststate != player_state) {
			if (laststate.D - laststate.A != player_state.D - player_state.A) {
				if (player_state.D - player_state.A === 0) {
					playAnimation(player_node, "Image/daiji", animatespeed, true, faceto, player_size);
				} else if (player_state.D - player_state.A === -1) {
					faceto = false;
					playAnimation(player_node, "Image/scientist", animatespeed, true, false, player_size);
				} else if (player_state.D - player_state.A === 1) {
					faceto = true;
					playAnimation(player_node, "Image/scientist", animatespeed, true, true, player_size);
				}
			}

			laststate = { ...player_state };
		}
		let evisible = false;
		if (canmove) {
			for (const [key, value] of pairs(npcprops)) {
				if (value.visible && value.tag != -1 && savefile.player_pos.x > (value.pos.x - value.size / 2) && savefile.player_pos.x < (value.pos.x + value.size / 2)) {
					eButton.node.visible = true;
					etag = value.tag;
					evisible = true;
				}
			}
			if (!evisible) {
				eButton.node.visible = false;
				etag = -1;
			}
		}
		sleep(0.2);
		return nowpage != 2;
	})
	const tl2 = threadLoop(() => {
		savefile.player_pos = savefile.player_pos.add(Vec2((player_state.D - player_state.A) * walkspeed, 0))
    .clamp(min_player_pos , max_player_pos );

		player_node.x = savefile.player_pos.x;
		setbackgroundposition();
		sleep(0.01);
		return nowpage != 2;
	})

	root.gslot("Input.Space", () => {
		skip = canskip;
	});

	root.gslot("Input.LeftDown", () => {
		if (canmove) {
			player_state.A = 1;
		}
	});
	root.gslot("Input.RightDown", () => {
		if (canmove) {
			player_state.D = 1;
		}
	});
	root.gslot("Input.LeftUp", () => {
		if (canmove) {
			player_state.A = 0;
		}
	});
	root.gslot("Input.RightUp", () => {
		if (canmove) {
			player_state.D = 0;
		}
	});
	root.onTapped(() => {
		skip = canskip;
	})
	return root;
}

const act_three = () => {

	nowpage = 3;
	savefile.act = 1;
	let firstdonghua = false;
	if (savefile.act != 2) {
		savefile.act = 2;
		savefile.player_pos = Vec2(490, 210);
	}
	saveManager.saveGame(nowslot, savefile);

	let animatespeed = 1 / walkspeed;
	let etag = -1;

	const background2 = Sprite("Image/第二幕/official/two_right.png") ?? Sprite();

	const player_size = Size(145, 340);
	let min_player_pos = Vec2(player_size.width / 2 + 450, 210);
	let max_player_pos = Vec2(background2.width - player_size.width / 2 , 210);
	const root = Node();

	const finish = (time: number) => {
		savefile.playTime = savefile.playTime + App.totalTime - gamestarttime;
		saveManager.saveGame(nowslot, savefile);
		thread(() => {
			appState.isCGing = true;
			Transition({
				root: Director.entry,
				time: time,
				type: -1,
				waittime: 0,
			})
			appState.isCGing = false;
			root.cleanup();
			root.removeAllChildren();
			root.removeFromParent();
		})
	}
	
	root.addChild(background2);
	let npcs: Node.Type[] = [];
	for (const [key, value] of pairs(npcprops)) {
		const npc = Node();
		npc.tag = value.tag.toString();
		npc.position = value.pos;
		npc.visible = value.visible;
		background2.addChild(npc);
		npcs.push(npc);
		playAnimation(npc, value.daiji, 0.4, true, false, Size(175, 350));
	}

	npcprops.lajitong.pos = Vec2(12000, 210);
	npcprops.guanggao1.pos = Vec2(12000, 210);
	npcprops.dianxiangan.pos = Vec2(1500, 210);

	const eClick = (switched?: boolean, tag?: number) => {
		if (etag != -1) {
			playAnimation(player_node, "Image/scientist", animatespeed, true, true, player_size);
			player_state.A = 0; player_state.D = 0;
			eButton.node.visible = false;
		}
		
		if (etag === npcprops.linzhi.tag) {
			thread(() => {
				canmove = false;
				playAnimation(player_node, "Image/daiji", 0.4, true, true, player_size);
				dialognode.setillustration(Illustration.wu);

				dialognode.node.visible = true;
				dialognode.setname("");
				dialognode.setcontent("这个人是谁？为什么会出现在这里？");

				sleep(0.5);

				canskip = true;
				wait(() => {
					return skip;
				});
				skip = false; canskip = false;

				dialognode.setname("？？？")
				dialognode.setcontent("云忆。");
				
				sleep(0.5);

				canskip = true;
				wait(() => {
					return skip;
				});
				skip = false; canskip = false;

				dialognode.setname("云忆")
				dialognode.setcontent("你是谁？你怎么知道我的名字？");
				
				sleep(1);

				canskip = true;
				wait(() => {
					return skip;
				});
				skip = false; canskip = false;
				
				dialognode.setname("？？？")
				dialognode.setcontent("我是林知，我知道我要帮你。你们的病症叫‘记忆断□症，而我知道你所有关于□的记忆。");
				
				sleep(1.5);

				canskip = true;
				wait(() => {
					return skip;
				});
				skip = false; canskip = false;
				
				dialognode.setname("")
				dialognode.setcontent("熟悉的眩晕感瞬间包围了我，那些曾经如云一般飘散的记忆逐渐向我靠拢。");
				
				sleep(1.5);

				canskip = true;
				wait(() => {
					return skip;
				});
				skip = false; canskip = false;

				finish(1);
				act_end();
			});
		} 
		else if (etag === npcprops.dianxiangan.tag) {
		thread(() => {
			canmove = false; dialognode.node.visible = true;
			playAnimation(player_node, "Image/daiji", 0.4, true, false, player_size);

			dialognode.setname("");
			dialognode.setcontent("“丁零零——”急促的电话铃声打断了我的思绪。");
			
			sleep(1);

			canskip = true;
			wait(() => {
				return skip;
			});
			skip = false; canskip = false;

			dialognode.setname("墨染");
			dialognode.setcontent("所长，所长！你在哪儿？");
						
			sleep(1);

			canskip = true;
			wait(() => {
				return skip;
			});
			skip = false; canskip = false;

			dialognode.setname("");
			dialognode.setcontent("她一向平静的声音竟满是惊慌。");
						
			sleep(1);

			canskip = true;
			wait(() => {
				return skip;
			});
			skip = false; canskip = false;

			dialognode.setname("云忆");
			dialognode.setcontent("怎么了？我还在——");
						
			sleep(0.5);

			canskip = true;
			wait(() => {
				return skip;
			});
			skip = false; canskip = false;
			
			dialognode.setname("墨染");
			dialognode.setcontent("您快回来吧！我们发现越来越多的研究员不认识□了！还有很多普通人，他们看不到□字，也不记得□！");
						
			sleep(1.5);

			canskip = true;
			wait(() => {
				return skip;
			});
			skip = false; canskip = false;
			
			dialognode.setname("云忆");
			dialognode.setcontent("别慌！我会尽快回去。");
						
			sleep(0.5);

			canskip = true;
			wait(() => {
				return skip;
			});
			skip = false; canskip = false;

			const zhiying = Label(fontName, 50);
			if (zhiying) {
				zhiying.text = "转身回去";
				zhiying.color = Color(0xff000000);
				zhiying.addTo(background2);
				zhiying.position = Vec2(savefile.player_pos.x, savefile.player_pos.y + 280);
				thread(() => {
					sleep(3);
					zhiying.removeFromParent();
				})
			}

			npcprops.dianxiangan.pos = Vec2(12000, 210);
			npcprops.linzhi.visible = true;

			let npcs: Node.Type[] = [];
			for (const [key, value] of pairs(npcprops)) {
				const npc = Node();
				npc.tag = value.tag.toString();
				npc.position = value.pos;
				npc.visible = value.visible;
				background2.addChild(npc);
				npcs.push(npc);
				playAnimation(npc, value.daiji, 0.4, true, false, Size(175, 350));
			}

			dialognode.node.visible = false;
			canmove = true;
		})
		}
	}
	const eButton = Button({
		// x: player_pos.x,
		// y: player_pos.y+200,
		x: 0,
		y: 50,
		width: 128,
		height: 128,
		onClick: eClick,
		normalImage: "Image/部件/E.png",
		pressImage: "Image/部件/E.png",
	});
	eButton.node.gslot("Input.EUp", () => {
		if (eButton.node.visible)
			eClick();
	})
	eButton.node.visible = false;
	eButton.node.addTo(root);

	const setbackgroundposition2 = () => {
		background2.x = background2.width / 2 - designSize.width / 2 - ((savefile.player_pos.x - designSize.width / 2) >= 0 && savefile.player_pos.x <= background2.width - designSize.width / 2 ? (savefile.player_pos.x - designSize.width / 2) : ((savefile.player_pos.x - designSize.width / 2) < 0 ? 0 : background2.width - designSize.width));
	}
	setbackgroundposition2();

	const dialognode = Dialog({
		name: "",
		illustration: Illustration.wu,
	})
	root.addChild(dialognode.node);
	dialognode.node.order = 1;

	const player_node = Node();
	player_node.addTo(background2);
	player_node.position = Vec2(800,210);

	dialognode.node.visible = false;
	canmove = true;

	thread(() => {
		canmove = false; dialognode.node.visible = true;
		playAnimation(player_node, "Image/daiji", 0.4, true, false, player_size);
		sleep(0.8);

		dialognode.setcontent("站在这边，望着对面，仍然是那座断了的建筑。");
					
		sleep(1);
		
		canskip = true;
		wait(() => {
			return skip;
		});
		skip = false; canskip = false;

		dialognode.setcontent("青石的棱角在粼粼河水中沉默着，好像想说什么，却欲言又止。");
							
		sleep(1.5);

		canskip = true;
		wait(() => {
			return skip;
		});
		skip = false; canskip = false;

		dialognode.setcontent("一种难以言喻的、陌生的熟悉感涌上心头。");
							
		sleep(1);

		canskip = true;
		wait(() => {
			return skip;
		});
		skip = false; canskip = false;

		dialognode.node.visible = false;
		canmove = true;
	})

	playAnimation(player_node, "Image/scientist", animatespeed, true, true, player_size);
	player_state = {
		A: 0,
		D: 0,
		B: 0,
		TAB: 0,
	};
	laststate = {
		A: 0,
		D: 0,
		B: 0,
		TAB: 0,
	}

  laststate = { ...player_state };
	let faceto = true;
	const tl3 = threadLoop(() => {
		if (laststate != player_state) {
			if (laststate.D - laststate.A != player_state.D - player_state.A) {
				if (player_state.D - player_state.A === 0) {
					playAnimation(player_node, "Image/daiji", animatespeed, true, faceto, player_size);
				} else if (player_state.D - player_state.A === -1) {
					faceto = false;
					playAnimation(player_node, "Image/scientist", animatespeed, true, false, player_size);
				} else if (player_state.D - player_state.A === 1) {
					faceto = true;
					playAnimation(player_node, "Image/scientist", animatespeed, true, true, player_size);
				}
			}

			laststate = { ...player_state };
		}
		let evisible = false;
		if (canmove) {
			for (const [key, value] of pairs(npcprops)) {
				if (value.visible && value.tag != -1 && savefile.player_pos.x > (value.pos.x - value.size / 2) && savefile.player_pos.x < (value.pos.x + value.size / 2)) {
					eButton.node.visible = true;
					etag = value.tag;
					evisible = true;
				}
			}
			if (!evisible) {
				eButton.node.visible = false;
				etag = -1;
			}
		}
		sleep(0.2);
		return nowpage != 3;
	})
	const tl4 = threadLoop(() => {
		savefile.player_pos = savefile.player_pos.add(Vec2((player_state.D - player_state.A) * walkspeed, 0))
    .clamp(min_player_pos , max_player_pos );

		player_node.x = savefile.player_pos.x;
		setbackgroundposition2();
		sleep(0.01);
		return nowpage != 3;
	})

	root.gslot("Input.Space", () => {
		skip = canskip;
	});

	root.gslot("Input.LeftDown", () => {
		if (canmove) {
			player_state.A = 1;
		}
	});
	root.gslot("Input.RightDown", () => {
		if (canmove) {
			player_state.D = 1;
		}
	});
	root.gslot("Input.LeftUp", () => {
		if (canmove) {
			player_state.A = 0;
		}
	});
	root.gslot("Input.RightUp", () => {
		if (canmove) {
			player_state.D = 0;
		}
	});
	root.onTapped(() => {
		skip = canskip;
	})
	return root;
}

const act_end = () => {
	savefile.act = 3;
	saveManager.saveGame(nowslot, savefile);
	Audio.stopStream(1);
	Audio.playStream(resources.sounds.end, true);
	const root = Node();
	thread(() => {
		appState.isCGing = true;
		Transition({
			root: Director.entry,
			time: 1,
			type: -1,
			waittime: 0,
		})
		appState.isCGing = false;
	})

	thread(() => {
		const xinzhi = Sprite("Image/第一幕/zhi.png");
		if (xinzhi) {
			xinzhi.size = Size(600, 660);
			xinzhi.onTapped(() => {
				skip = true;
			})
		}
		
		let xincontent = Label(fontName, 30);
		if (xincontent) {
			xincontent.text = "    我看见——\n    很多年前，木头搭的通道被洪水冲走了。\n    于是，人们从山里运来青石，一块一块垒上去。\n    没有谁召集，但每个人都无声地劳动着。\n    新的就这样造好，连接了两岸的集市和人家。\n    我看见——\n    一群孩童嬉笑着跑过，又在青石板上不小心摔个跟头。\n    后面的大人慢悠悠地跟着，偶尔调侃一句“这里真该修了”。\n    往来的商贩总爱在顶上停下，稍事歇息后继续赶路。\n    而我，总是被稀奇的小玩意吸引，缠着父母买回家。\n    qiao，qiao，qiao……\n    原来，是桥啊。";
			xincontent.textWidth = 500;
			xincontent.color = Color(0xff000000)
			xincontent.alignment = TextAlign.Left;
		}
							
		sleep(7);

		canskip = true;
		wait(() => { sleep(0.1); return skip; });
		skip = false; canskip = false;
		if (xinzhi)
			xinzhi.removeFromParent();
		if (xincontent)
			xincontent.removeFromParent();
		
		const dialognode = Dialog({
			name: "林知",
			illustration: Illustration.wu,
		})
		root.addChild(dialognode.node);
		dialognode.node.visible = true;

		dialognode.setcontent("所以现在最重要的，是阻止记忆断桥症的蔓延。");
							
		sleep(1);

		canskip = true;
		wait(() => {
			return skip;
		});
		skip = false; canskip = false;
		
		dialognode.setname("云忆");
		dialognode.setcontent("这个断桥症……到底是什么？");
							
		sleep(0.5);

		canskip = true;
		wait(() => {
			return skip;
		});
		skip = false; canskip = false;
		
		dialognode.setname("林知");
		dialognode.setcontent("说来话长。");
							
		sleep(0.5);

		canskip = true;
		wait(() => {
			return skip;
		});
		skip = false; canskip = false;
		dialognode.node.visible = false;
		

		const xinzhi1 = Sprite("Image/第一幕/zhi.png");
		if (xinzhi1) {
			xinzhi1.size = Size(600, 660);
			xinzhi1.onTapped(() => {
				skip = true;
			})
		}
		
		let xincontent1 = Label(fontName, 30);
		if (xincontent1) {
			xincontent1.text = "    在林知的讲述下，我终于得知，这次精神实验触及了人脑内的“桥”——连接集体潜意识的神经元。\n    而这个契机，让神经元觉醒了自主意识。\n    作为那座“原初之桥”，它最重要的使命，便是连接人们的潜意识，让人们得以互动、交流。\n    于是，原本如孤岛一般的人，终于，建立了联系。\n";
			xincontent1.textWidth = 500;
			xincontent1.color = Color(0xff000000)
			xincontent1.alignment = TextAlign.Left;
		}
							
		sleep(5);

		canskip = true;
		wait(() => { sleep(0.1); return skip; });
		skip = false; canskip = false;

		if (xincontent1) {
			xincontent1.text = "    像海洋深处的鲸鱼，第一次接收到了相同的频率。\n    那种无法掩饰的激动与喜悦，成为了它存在的意义。\n    这时的它是欢喜的，因为它在人与人之间看到了无数的“桥”——亲人，朋友，恋人……\n    人们的关系越来越亲密，善意之“桥”不断拯救孤独的灵魂，原本冰冷的心越靠越近，于是人间温情就此孵化。";
			xincontent1.textWidth = 500;
			xincontent1.color = Color(0xff000000)
			xincontent1.alignment = TextAlign.Left;
		}
							
		sleep(5);

		canskip = true;
		wait(() => { sleep(0.1); return skip; });
		skip = false; canskip = false;

		if (xincontent1) {
			xincontent1.text = "    但……它发现，恶意之桥出现了，并不断侵蚀着原本的‘桥’。\n    怀疑、背叛、误解……\n    渐渐地，那一座座微小的桥，无可奈何地崩塌了。\n    它……很痛苦，可它只能……眼睁睁看着，曾经志同道合的人们，一步步背道而驰。\n    似乎，这个世界正在变回原本的样子。\n     人们只不再关注曾经那样深刻的联系与羁绊——亦不愿再去呵护这些“桥”。";
			xincontent1.textWidth = 500;
			xincontent1.color = Color(0xff000000)
			xincontent1.alignment = TextAlign.Left;
		}
							
		sleep(7);

		canskip = true;
		wait(() => { sleep(0.1); return skip; });
		skip = false; canskip = false;
		
		if (xincontent1) {
			xincontent1.text = "    就像……断桥镇年久失修的桥一样，这些‘桥’，塌了。而最初的神经元，在觉醒了自主意识后，最终决定……将自己分解，直到……人们重新珍视这来之不易的联系。\n    人们会患上‘记忆断桥症’，会忘记‘连接’与‘桥’的概念。最后，彻底变回与世隔绝的孤岛。";
			xincontent1.textWidth = 500;
			xincontent1.color = Color(0xff000000)
			xincontent1.alignment = TextAlign.Left;
		}
							
		sleep(4);

		canskip = true;
		wait(() => { sleep(0.1); return skip; });
		skip = false; canskip = false;
		
		if (xincontent1) {
			xincontent1.text = "    我问林知，“那，要怎样才能恢复？”\n    \n    林知说，我要重新做一次实验，去寻找那座原初之桥。";
			xincontent1.textWidth = 500;
			xincontent1.color = Color(0xff000000)
			xincontent1.alignment = TextAlign.Left;
		}
							
		sleep(2);

		canskip = true;
		wait(() => { sleep(0.1); return skip; });
		skip = false; canskip = false;
			
		if (xincontent1) {
			xincontent1.text = "    第二次精神连接实验，成功了。\n    我找到了那座原初之桥的碎片，而拼接的代价——\n    是我和林知以自身为媒介，合二为一，作为这个新世界的观察者与监督者，默默守护人世间一座座温暖的，桥。\n    \n    新的原初之桥存在于所有人的潜意识中，它不是木质的，也不是石质的，而是——\n    一座连接万物的云桥。";
			xincontent1.textWidth = 500;
			xincontent1.color = Color(0xff000000)
			xincontent1.alignment = TextAlign.Left;
		}
							
		sleep(5);

		canskip = true;
		wait(() => { sleep(0.1); return skip; });
		skip = false; canskip = false;
			
		if (xincontent1) {
			xincontent1.text = "    我与林知，则是桥上飘过的第一朵云。\n    我们飘过断桥镇——\n    夕阳西下，人们自发聚集起来，再一次从山上搬下青石板，一点一点，将那座坍塌的断桥留下的空白填补起来。\n    从此，断桥镇啊，便改了名。\n    “云桥镇”。";
			xincontent1.textWidth = 500;
			xincontent1.color = Color(0xff000000)
			xincontent1.alignment = TextAlign.Left;
		}
							
		sleep(5);

		canskip = true;
		wait(() => { sleep(0.1); return skip; });
		skip = false; canskip = false;
			
		if (xincontent1) {
			xincontent1.text = "    于是——\n    久违的笑声盘旋而上。\n    击掌庆贺的瞬间，一座座“桥”——\n    静悄悄地，跨过隔阂、跨过偏见，\n    带着温暖的光,向意识深处绵延而去。\n    如彩虹般绚烂，生生不息。\n    于是这世界不再是孤岛，亦不再冰冷——\n    岁月的余温，一如往昔。";
			xincontent1.textWidth = 500;
			xincontent1.color = Color(0xff000000)
			xincontent1.alignment = TextAlign.Left;
		}
							
		sleep(5);

		canskip = true;
		wait(() => { sleep(0.1); return skip; });
		skip = false; canskip = false;
			
		if (xincontent1) {
			xincontent1.text = "    我叫云忆。\n    你或许不认识我，但——\n    你的意识深处，\n    在那座星光点点的桥上，\n    一定飘着，一朵云。\n    \n    当人与人之间的“桥”被忽视或摧毁，记忆与文明将随之断裂；唯有主动重建理解与信任，才能让联结生生不息。";
			xincontent1.textWidth = 500;
			xincontent1.color = Color(0xff000000)
			xincontent1.alignment = TextAlign.Left;
		}
							
		sleep(4);

		canskip = true;
		wait(() => { sleep(0.1); return skip; });
		skip = false; canskip = false;
		
		if (xinzhi1)
			xinzhi1.removeFromParent();
		if (xincontent1)
			xincontent1.removeFromParent();
		
		const lb = Label(fontName, 40);
		if (lb) {
			lb.text = "感谢各位玩家的游玩！由于本小组水平有限，未能实现有意思的玩法，但我们小组尽全力为您带来最好的剧情阅读体验。未来，我们也希望继续努力，把我们最初的游戏玩法尝试实现出来，也欢迎玩家们提出宝贵意见。\n最后，感谢dora开发者提供的技术支持，感谢小组的每一个小伙伴！"
			lb.textWidth = 800;
		}

	});
	root.gslot("Input.Space", () => {
		skip = canskip;
	});
	root.onTapped(() => {
		skip = canskip;
	})
	return root;
}

// 初始化
updateViewSize();
adjustWinSize();
Director.entry.onAppChange(settingName => {
	if (settingName === 'Size') {
		updateViewSize();
	}
});

// 启动场景
StartUp();